package CGW.controller;

import CGW.bean.Cart;
import CGW.bean.Food;
import CGW.bean.User;
import CGW.dao.FoodDao;
import CGW.service.FoodService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/cart")
public class CartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String action  =


        int table_id = Integer.parseInt(req.getParameter("table"));
        HttpSession session = req.getSession();
        int user_id = ((User)session.getAttribute("login_user")).getUser_id();
        String cartName = "cart-"+user_id+"-"+table_id;
        List<Cart> cartItems = new ArrayList<>();
        double totalPrice = 0;
        int total_sum = 0;
        if(session.getAttribute(cartName)!=null){
            FoodService service = new FoodService(new FoodDao());
            Map<Integer, Integer> cart = (Map<Integer, Integer>) session.getAttribute(cartName);
            for (Integer food_id: cart.keySet()){
                Food food = service.get(food_id);
                cartItems.add(new Cart(food_id,food, table_id, cart.get(food_id)));
                totalPrice += food.getFood_price()*cart.get(food_id).doubleValue();
                total_sum++;
            }
            service.close();
        }
        req.getSession().setAttribute("total_sum", total_sum);
        req.setAttribute("total_sum",total_sum);
        req.setAttribute("cartName",cartName);
        req.setAttribute("cartItems", cartItems);
        req.getSession().setAttribute("cartItems", cartItems);
        req.setAttribute("totalPrice", totalPrice);
        req.setAttribute("table_id", table_id);
        req.getSession().setAttribute("table_id", table_id);
        req.setAttribute("user_id",user_id);
        req.getRequestDispatcher("cart.jsp").forward(req, resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int table_id = Integer.parseInt(req.getParameter("table"));
        int food_id = Integer.parseInt(req.getParameter("food"));
        String action = req.getParameter("action"); // add, sub
        HttpSession session = req.getSession();
        int user_id = ((User)session.getAttribute("login_user")).getUser_id();
        Map<Integer, Integer> cart = null;
        String cartName = "cart-"+user_id+"-"+table_id;
        if(session.getAttribute(cartName) != null){
            cart = (Map<Integer, Integer>) session.getAttribute(cartName);
        }else{
            cart = new HashMap<>();
        }
        if(action.equals("add")){
            if(cart.isEmpty() || ! cart.containsKey(food_id)){
                cart.put(food_id, 1);
            }else{
                cart.put(food_id, cart.get(food_id)+1);
            }
        }else{
            if(! cart.isEmpty()){
                cart.put(food_id, cart.get(food_id)-1);
            }
        }

        session.setAttribute(cartName, cart);
        resp.getWriter().write("ok");
    }
}
